package com.shire.shire_interface.controller.group;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroupMember;
import com.shire.shire_interface.model.group.WxGroupMemberExample;
import com.shire.shire_interface.service.group.WxGroupMemberService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Api(tags = "加好友 添加群好友")
@RestController
@RequestMapping("/group")
public class WxGroupMemberController extends BaseController {

    @Resource
    private WxGroupMemberService wxGroupMemberService;

//    @ApiOperation(value = "获取单个群的成员 csId, wxAccount, groupNo")
    @ResponseBody
    @PostMapping(value = "/groupMember")
    public Response groupMembers(@RequestBody JSONObject jsonObject, HttpSession session) {

        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            String groupNo = jsonObject.getString("groupNo");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxGroupMemberExample example = new WxGroupMemberExample();
            example.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(groupNo);
            List<WxGroupMember> wxGroupMembers = wxGroupMemberService.groupMembers(example);
            response.setData(wxGroupMembers);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "获取多个群的成员 csId, wxAccount, groupNoList")
    @ResponseBody
    @PostMapping(value = "/groupMemberByGroupNoList")
    public Response groupsMembers(@RequestBody JSONObject jsonObject, HttpSession session) {

        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            List<String> groupNos = (List<String>) jsonObject.get("groupNoList");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            Map<String, Object> resultMap = new HashMap<>();
            WxGroupMemberExample example = new WxGroupMemberExample();
            for (String groupNo : groupNos) {
                example.clear();
                example.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(groupNo);
                List<WxGroupMember> wxGroupMembers = wxGroupMemberService.groupMembers(example);
                resultMap.put(groupNo,wxGroupMembers);
            }
            response.setData(resultMap);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
